//
//  LYNavigationBar.m
//  Pods
//
//  Created by youger on 7/11/15.
//
//

#import "LYNavigationBar.h"
#import <Masonry.h>
#import <objc/runtime.h>

@interface LYNavigationBar()

@property (strong, nonatomic) UILabel  * titleLabel;
@property (strong, nonatomic) UIView   * contentView;
@property (strong, nonatomic) NSMutableArray * navBarLeftItems;
@property (strong, nonatomic) NSMutableArray * navBarRightItems;

@end

@implementation LYNavigationBar
{
    NSLayoutConstraint * _titleLabelLeftConstraint;
    NSLayoutConstraint * _titleLabelRightConstraint;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _lineView = [UIView new];
        _lineView.backgroundColor = HEXCOLOR(0xeeeeee);
        [self.contentView addSubview:_lineView];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_contentView);
            make.height.mas_equalTo(0.5);
        }];
        
        self.navBarLeftItems = [[NSMutableArray alloc] init];
        self.navBarRightItems = [[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark - Setter

- (void)setTitleColor:(UIColor *)titleColor
{
    self.titleLabel.textColor = titleColor;
}

- (void)setTitleText:(NSString *)titleText
{
    self.titleLabel.text = titleText;
    CGSize titleSize = [_titleLabel intrinsicContentSize];
    titleSize.width = SCREEN_WIDTH - 2 * Nav_Bar_Button_Width;
    
    [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(titleSize);
    }];
}

- (void)setTitleView:(UIView *)titleView
{
    if (_titleView != titleView) {
        
        _titleView = titleView;
        [self.contentView addSubview:_titleView];
        
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(_contentView);
        }];
    }
}

#pragma mark - getter

- (UIView *)contentView
{
    if (_contentView == nil) {
        
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor clearColor];
        [self addSubview:_contentView];
        
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.right.and.bottom.equalTo(self);
            make.height.mas_equalTo(Nav_Bar_Button_Height);
        }];
    }
    return _contentView;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = HEXCOLOR(0x666666);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:18.f];
        
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.centerX.centerY.equalTo(_contentView);
        }];
        
        _titleView = _titleLabel;
    }
    return _titleLabel;
}

- (UIButton *)navBarButton
{
    UIButton * navBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [navBarButton setTitleColor:HEXCOLOR(0x666666) forState:UIControlStateNormal];
    [navBarButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    navBarButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
    
    [navBarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        CGSize buttonSize = CGSizeMake(Nav_Bar_Button_Width, Nav_Bar_Button_Height);
        make.size.mas_equalTo(buttonSize).with.priorityHigh();
    }];
    return navBarButton;
}

- (UIImageView *)backgroundView
{
    if (_backgroundView == nil) {
        
        _backgroundView = [[UIImageView alloc] init];
        [self insertSubview:_backgroundView belowSubview:self.contentView];
        
        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.right.and.bottom.equalTo(self);
        }];
    }
    return _backgroundView;
}

- (void)layoutNavBarLeftItem:(UIButton *)navButton
{
    NSInteger count = _navBarLeftItems.count;
    
    CGFloat leftOffset = count * Nav_Bar_Button_Width;
    
    [navButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_contentView).offset(leftOffset);
        make.bottom.equalTo(_contentView);
    }];

    if (count == 0) {
        self.leftBarButton = navButton;
    }
}

- (void)layoutNavBarRightItem:(UIButton *)navButton
{
    NSInteger count = _navBarRightItems.count;
    
    CGFloat rightOffset = -(count * Nav_Bar_Button_Width);
    [navButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_contentView).offset(rightOffset);
        make.bottom.equalTo(_contentView);
    }];
    
    if (count == 0){
        self.rightBarButton = navButton;
    }
}

- (UIButton *)setNavBarButtonTitle:(NSString *)title image:(NSString *)imageName target:(id)target action:(SEL)action
{
    UIButton * navBarButton = [self navBarButton];
    [navBarButton setTitle:title forState:UIControlStateNormal];
    if (imageName) {
        [navBarButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    [navBarButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:navBarButton];
    
    return navBarButton;
}

- (UIButton *)setLeftButtonTitle:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton * leftButton = [self setNavBarButtonTitle:title image:nil target:target action:action];
    [self layoutNavBarLeftItem:leftButton];
    [self.navBarLeftItems addObject:leftButton];
    
    return leftButton;
}

- (UIButton *)setRightButtonTitle:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton * rightButton = [self setNavBarButtonTitle:title image:nil target:target action:action];
    
    [self layoutNavBarRightItem:rightButton];
    [self.navBarRightItems addObject:rightButton];
    
    CGSize buttonSize = [rightButton intrinsicContentSize];
    if (buttonSize.width > Nav_Bar_Button_Width) {
        
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(buttonSize.width + 10);
        }];
    }
    return rightButton;
}

- (UIButton *)setLeftButtonImage:(NSString *)imageName target:(id)target action:(SEL)action stretchableWidth:(CGFloat)stretchableWidth
{
    UIButton * leftButton = [self setNavBarButtonTitle:nil image:imageName target:target action:action];
    
    [self layoutNavBarLeftItem:leftButton];
    [self.navBarLeftItems addObject:leftButton];
    
    return leftButton;
}

- (UIButton *)setRightButtonImage:(NSString *)imageName target:(id)target action:(SEL)action stretchableWidth:(CGFloat)stretchableWidth
{
    UIButton * rightButton = [self setNavBarButtonTitle:nil image:imageName target:target action:action];
    
    [self layoutNavBarRightItem:rightButton];
    [self.navBarRightItems addObject:rightButton];
    
    return rightButton;
}

- (UIButton *)setLeftButtonImage:(NSString *)imageName highlighted:(NSString*)highlightedImage target:(id)target action:(SEL)action
{
    UIButton * leftButton = [self setLeftButtonImage:imageName target:target action:action stretchableWidth:0];
    [leftButton setImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    
    return leftButton;
}

- (UIButton *)setRightButtonImage:(NSString *)imageName highlighted:(NSString*)highlightedImage target:(id)target action:(SEL)action
{
    UIButton * rightButton = [self setRightButtonImage:imageName target:target action:action stretchableWidth:0];
    [rightButton setImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    
    return rightButton;
}

@end


static char LYNavigationBarKey;

@implementation LYRootViewController(NavigationBar)

- (void)setLy_navigationBar:(LYNavigationBar *)naviBar
{
    objc_setAssociatedObject(self, &LYNavigationBarKey, naviBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (LYNavigationBar *)ly_navigationBar
{
    LYNavigationBar * naviBar = objc_getAssociatedObject(self, &LYNavigationBarKey);
    if (naviBar == nil) {
        
        naviBar = [self createNavigationBar];
        [self setLy_navigationBar:naviBar];
    }
    return naviBar;
}

- (LYNavigationBar *)createNavigationBar
{
    LYNavigationBar * naviBar = [[LYNavigationBar alloc] init];
    naviBar.backgroundColor = [UIColor whiteColor];
    self.ly_navigationBar = naviBar;
    [self.view addSubview:naviBar];
    
    [naviBar mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.and.top.equalTo(self.view);
        make.height.mas_equalTo(Nav_Bar_Height);
    }];
    
    return naviBar;
}

#pragma mark - Action

- (void)setBackButton
{
    [self.ly_navigationBar setLeftButtonImage:@"global_back" target:self action:@selector(backAction) stretchableWidth:0];
}

- (void)backAction:(BOOL)animation
{
    if (self.navigationController == nil || self.navigationController.viewControllers.count == 1) {
        [self dismissViewControllerAnimated:animation completion:NULL];
    }else{
        [self.navigationController popViewControllerAnimated:animation];
    }
}

- (void)backAction
{
    [self backAction:YES];
}


@end
